package com.smartmining.v.utils;

import com.smartmining.base.utils.SmartUtils;
import com.smartmining.v.manager.VMOperation;

import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;


/**
 */
public class StringUtil {
	/**
	 * 首字母大写
	 * @param str
	 * @return
	 */
    public static String upperCaseFirstChar(String str){
    	char[] ch = str.toCharArray();
    	if (ch[0] >= 'a' && ch[0] <= 'z') {
    		ch[0] = (char) (ch[0] - 32);
    	}
    	return new String(ch);
    }
    /***
     * 字符串转指类型
     * @param val
     * @param type
     * @return
     */
	public static <T>T getVal(String val, Class<T> type) {
		T value = null;
		try {
			Constructor<T> constructor = type.getConstructor(String.class);
			constructor.setAccessible(true);
			value = constructor.newInstance(val);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return value;
	}


	public static List<String> praseStrByRegex(String str, String regex){
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(str);
		List<String> list = new ArrayList<String>();
		Set<String> aliasSet = new HashSet<String>();
		while(matcher.find()){
			String alias = str.substring(matcher.start(),matcher.end());
			if(!aliasSet.contains(alias)){
				list.add(alias);
				aliasSet.add(alias);
			}
		}
		return list;
	}

	public static List<String> praseStrByRegex(String str,String regex,int[] placeholder){
		List<String> result = new ArrayList<String>();
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(str);
		while(matcher.find()){
			String alias = str.substring(matcher.start()+placeholder[0],matcher.end()-placeholder[1]);
			result.add(alias);
		}
		return result;
	}

	
	public static String rvZeroAndDot(String s) {
		if (s.isEmpty()) {
			return null;
		}
		if (s.indexOf(".") > 0) {
			s = s.replaceAll("0+?$", "");//去掉多余的0
			s = s.replaceAll("[.]$", "");//如最后一位是.则去掉
		}
		return s;
	}

	public static String substrToLastUnderLine(String str){
		if(str == null || str.indexOf(VMOperation.connector)==-1){
			return str;
		}else{
			int index = str.lastIndexOf(VMOperation.connector);
			return str.substring(0,index);
		}
	}

	public static String wrapReplaceStr(String str,String regex,int[] placeholder,String... warps){
		String wrap1 = "";
		String wrap2 = "";
		if(warps!=null){
			if(warps.length>=1){
				wrap1 =warps[0];
			    if(warps.length > 1) {
					wrap2 =warps[1];
				}
			}
		}
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(str);
		StringBuffer sb = new StringBuffer();
		while(matcher.find()){
			String alias = str.substring(matcher.start()+placeholder[0],matcher.end()-placeholder[1]);
			matcher.appendReplacement(sb,wrap1+alias+wrap2);
		}
		if(sb.length() > 0){
			matcher.appendTail(sb);
			return sb.toString();
		}else{
			return str;
		}
	}


	/**
	 * 获取保留小数位后的小数(字符串)
	 * @param source
	 * @param decimalNum 要保留的小数位数
	 * @return
	 */
	public static String getFormatNum(String source, int decimalNum) {
		if (SmartUtils.isNotNullAndBlank(source) && decimalNum >= 0) {
			double v = Double.parseDouble(source);
			return String.format("%." + decimalNum + "f", v);
		} else {
			return source;
		}
	}

	/**
	 * 判断字符串是否是 数值型
	 * @param str
	 * @return
	 */
	public static boolean isNumberStr(String str) {
		if (str != null) {
			return str.matches("-?[0-9]+.?[0-9]*");
		}
		return false;
	}

        /**
     * 获取当前日期时间
     * @return  字符串 14位
     */
    public static String getCurrDateTimeStr() {
        Date date = new Date();
        DateFormat df = new SimpleDateFormat("yyyyMMddhhmmss");
        return df.format(date);
    }

    /**
     * 获取随机字符串 32位
     * @return
     */
    public static String getUUIDStr() {
        UUID uuid = UUID.randomUUID();
        return uuid.toString().replace("-", "");
    }




}
